package patients

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/patients"
	patientsReq "github.com/flipped-aurora/gin-vue-admin/server/model/patients/request"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type PatientApi struct{}

// CreatePatient 创建patient表
// @Tags Patient
// @Summary 创建patient表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body patients.Patient true "创建patient表"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /patient/createPatient [post]
func (patientApi *PatientApi) CreatePatient(c *gin.Context) {
	var patient patients.Patient
	err := c.ShouldBindJSON(&patient)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	patient.CreatedBy = utils.GetUserID(c)
	err = patientService.CreatePatient(&patient)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeletePatient 删除patient表
// @Tags Patient
// @Summary 删除patient表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body patients.Patient true "删除patient表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /patient/deletePatient [delete]
func (patientApi *PatientApi) DeletePatient(c *gin.Context) {
	ID := c.Query("ID")
	userID := utils.GetUserID(c)
	err := patientService.DeletePatient(ID, userID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeletePatientByIds 批量删除patient表
// @Tags Patient
// @Summary 批量删除patient表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /patient/deletePatientByIds [delete]
func (patientApi *PatientApi) DeletePatientByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	userID := utils.GetUserID(c)
	err := patientService.DeletePatientByIds(IDs, userID)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdatePatient 更新patient表
// @Tags Patient
// @Summary 更新patient表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body patients.Patient true "更新patient表"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /patient/updatePatient [put]
func (patientApi *PatientApi) UpdatePatient(c *gin.Context) {
	var patient patients.Patient
	err := c.ShouldBindJSON(&patient)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	patient.UpdatedBy = utils.GetUserID(c)
	err = patientService.UpdatePatient(patient)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindPatient 用id查询patient表
// @Tags Patient
// @Summary 用id查询patient表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询patient表"
// @Success 200 {object} response.Response{data=patients.Patient,msg=string} "查询成功"
// @Router /patient/findPatient [get]
func (patientApi *PatientApi) FindPatient(c *gin.Context) {
	ID := c.Query("ID")
	repatient, err := patientService.GetPatient(ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(repatient, c)
}

// GetPatientList 分页获取patient表列表
// @Tags Patient
// @Summary 分页获取patient表列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query patientsReq.PatientSearch true "分页获取patient表列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /patient/getPatientList [get]
func (patientApi *PatientApi) GetPatientList(c *gin.Context) {
	var pageInfo patientsReq.PatientSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := patientService.GetPatientInfoList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetPatientPublic 不需要鉴权的patient表接口
// @Tags Patient
// @Summary 不需要鉴权的patient表接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /patient/getPatientPublic [get]
func (patientApi *PatientApi) GetPatientPublic(c *gin.Context) {
	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	patientService.GetPatientPublic()
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的patient表接口信息",
	}, "获取成功", c)
}

func (patientApi *PatientApi) GetPatientGroups(c *gin.Context) {
	patient := patientService.GetPatientGroups()
	response.OkWithDetailed(patient, "获取成功", c)
}

func (patientApi *PatientApi) GetDepartmentGroups(c *gin.Context) {
	patient := patientService.GetDepartmentGroups()
	response.OkWithDetailed(patient, "获取成功", c)
}
